import Mock, { Random } from 'mockjs';
import { output } from '../help';


const getCount = (list, type) => {
  let count = 0;
  list.forEach(item => {
    if (Number(item[type])) {
      count += item[type];
    }
  });

  return count;
};

Mock.mock(/mock.getChartData/, () => {

  let viewData = [0, 1, 2].map((item) => {
    return {
      date: `2018-5-${2 + item}`,
      pv: Random.integer(10000, 15000),
      uv: Random.integer(7000, 9000),
    };
  });
  let awardData = [0, 1, 2, 3].map(() => {
    return {
      title: Random.ctitle(5, 8),
      percent: Random.integer(20, 80),
    };
  });

  return output({
    viewData: {
      pvTotal: getCount(viewData, 'pv'),
      uvTotal: getCount(viewData, 'uv'),
      list: viewData
    },
    awardData: {
      percentTotal: getCount(awardData, 'percent') / awardData.length,
      list: awardData,
    },
  });
});



